﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Diagnostics;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml.Linq;

namespace myDiet
{
    public partial class profile : PhoneApplicationPage
    {
        App app = App.Current as App;                   //set "app" for global variable [user profile]
        PageOrientation lastOrientation;                //set "lastOrientation" for detect page orientation
        UserData userItem;

        public profile()
        {
            InitializeComponent();
            this.OrientationChanged += new EventHandler<OrientationChangedEventArgs>(LoginPage_OrientationChanged);
            lastOrientation = this.Orientation;

            // open userXML.xml file and read data
            string test;
            try
            {
                // set variable refer to IsolatedStorageFile
                using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    // set variable refer to IsolatedStorageFileStream
                    IsolatedStorageFileStream isoFileStream = myIsolatedStorage.OpenFile("userXML.xml", FileMode.Open);
                    using (StreamReader reader = new StreamReader(isoFileStream))
                    {
                        // read to end file
                        test = reader.ReadToEnd();


                        // set element for feed data from userXML.xml file
                        XElement element = XElement.Parse(test);
                        var items = from item in element.Descendants("user")
                                    select userItem = new UserData
                                    {
                                        UserName = item.Element("username").Value,
                                        Password = item.Element("password").Value,
                                        Name = item.Element("name").Value,
                                        Gender = item.Element("gender").Value,
                                        Age = item.Element("age").Value,
                                        Email = item.Element("email").Value,
                                        BirthDay = item.Element("birthday").Value,
                                        Blood = item.Element("blood").Value,
                                        Disease = item.Element("disease").Value,
                                        Height = item.Element("height").Value,
                                        Weight = item.Element("weight").Value,
                                        Limit = item.Element("limit_cal").Value
                                    };

                        // check items
                        int count = items.Count();
                        if (count == 1)
                        {
                            string result = "Name : " + userItem.Name + "\n" +                   //set format for display to TextBlock
                           "Gender : " + userItem.Gender + "\n" +
                           "Birthday : " + userItem.BirthDay + "\n" +
                           "Age : " + userItem.Age + "\n" +
                           "Blood : " + userItem.Blood + "\n" +
                           "Disease : " + userItem.Disease + "\n" +
                           "Height : " + userItem.Height + " cm." + "\n" +
                           "Weight : " + userItem.Weight + " kg." + "\n" +
                           "Calorie Limit : " + userItem.Limit + " kcal." + "\n" +
                           "e-mail : " + userItem.Email + "\n";

                            ProfileResultTextBlock.Text = result;               //show output in TextBlock
                        }
                        else MessageBox.Show("can not find userXML.xml file");
                    }
                }
            }
            catch { MessageBox.Show("File not found !"); }
        }

        void LoginPage_OrientationChanged(object sender, OrientationChangedEventArgs e)         //detect and handle page orientation
        {
            PageOrientation newOrientation = e.Orientation;
            Debug.WriteLine("New orientation: " + newOrientation.ToString());

            RotateTransition transitionElement = new RotateTransition();
            switch (newOrientation)
            {
                case PageOrientation.Landscape:
                case PageOrientation.LandscapeRight:
                    // Come here from PortraitUp (i.e. clockwise) or LandscapeLeft?
                    if (lastOrientation == PageOrientation.PortraitUp)
                        transitionElement.Mode = RotateTransitionMode.In90Counterclockwise;
                    else
                        transitionElement.Mode = RotateTransitionMode.In180Clockwise;
                    break;
                case PageOrientation.LandscapeLeft:
                    // Come here from LandscapeRight or PortraitUp?
                    if (lastOrientation == PageOrientation.LandscapeRight)
                        transitionElement.Mode = RotateTransitionMode.In180Counterclockwise;
                    else
                        transitionElement.Mode = RotateTransitionMode.In90Clockwise;
                    break;
                case PageOrientation.Portrait:
                case PageOrientation.PortraitUp:
                    // Come here from LandscapeLeft or LandscapeRight?
                    if (lastOrientation == PageOrientation.LandscapeLeft)
                        transitionElement.Mode = RotateTransitionMode.In90Counterclockwise;
                    else
                        transitionElement.Mode = RotateTransitionMode.In90Clockwise;
                    break;
                default:
                    break;
            }

            // Execute the transition
            PhoneApplicationPage phoneApplicationPage = (PhoneApplicationPage)(((PhoneApplicationFrame)Application.Current.RootVisual)).Content;
            ITransition transition = transitionElement.GetTransition(phoneApplicationPage);
            transition.Completed += delegate
            {
                transition.Stop();
            };
            transition.Begin();

            lastOrientation = newOrientation;
        }

        private void history_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //navigate to "History" page
            NavigationService.Navigate(new Uri("/HistoryPage.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}
